package ${file.packageName};

import com.ezbuy.common.factory.ServiceFactory;
import com.ezbuy.common.functions.Consumer;
import com.ezbuy.common.helper.LogUtils;
import com.ezbuy.common.request.RequestSubject;
import com.ezbuy.common.request.RequestSubjectImpl;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ${name}ServiceTest {

    @Before
    public void setUp() throws Exception {
        //注册服务,建议放在Application,${name}WebServiceImpl的实例会在需要时才初始化,并有弱引用的缓存,防止内存泄露
        ServiceFactory.registerService(${name}Service.class, ${name}ServiceImpl.class);
    }

    @After
    public void tearDown() throws Exception {
        //反注册服务
        ServiceFactory.unRegisterService(${name}Service.class);
    }

    <#list methods as method>
    @Test
    public void ${method.testName}() throws Exception {
        LogUtils.print("开始执行:${method.testName}");
        long time = System.currentTimeMillis();
        //模拟Activity,我们最好在BaseActivity和BaseFragment实现此接口
        RequestSubject activity = new RequestSubjectImpl();
        //模拟3秒的网络请求
        final CountDownLatch countDownLatch=new CountDownLatch(1);
        ${method.inputType} req = new ${method.inputType}();
        ServiceFactory.getService(${name}Service.class).${method.name}(req, new Consumer<${method.outputType}>() {
            @Override
            public void consume(${method.outputType} resp) {
                LogUtils.print("请求成功:" + resp);
                countDownLatch.countDown();
            }
        }).register(activity);
        //其实不register也可以,但是那就不会自动取消了,回来时如果Activity退出了,Consumer的回调里面可能抛出空指针异常
        //模拟5秒之后,退出Activity
        TimeUnit.SECONDS.sleep(5);
        LogUtils.print("退出Activity");
        activity.notifyAllObservers();
        countDownLatch.await();
        LogUtils.print("结束执行:${method.testName}" + ",耗时:" + (System.currentTimeMillis() - time));
    }

    </#list>
}